// https://leetcode.cn/problems/permutations/description/

// 算法思路总结：
// 1. 回溯算法生成数组所有排列
// 2. 使用check数组标记已使用元素，避免重复选择
// 3. 路径长度等于数组大小时，保存当前排列
// 4. 递归前后进行路径的push和pop操作（回溯）
// 5. 时间复杂度：O(n×n!)，空间复杂度：O(n)（递归栈深度）

#include <iostream>
using namespace std;

#include <cstring>
#include <vector>
#include <algorithm>

class Solution 
{
public:
    vector<vector<int>> ret;
    vector<int> path;
    int m;
    bool check[7] = {false};
    vector<vector<int>> permute(vector<int>& nums) 
    {
        m = nums.size();
        ret.clear();
        memset(check, 0, sizeof(check));
        dfs(nums);

        return ret;
    }

    void dfs(vector<int>& nums)
    {
        if (path.size() == m)
        {
            ret.push_back(path);
            return ;
        }

        for (int i = 0 ; i < m ; i++)
        {
            if (check[i] == false)
            {
                path.push_back(nums[i]);
                check[i] = true;
                dfs(nums);
                path.pop_back();
                check[i] = false;
            }
        }
    }
};
void printResult(const vector<vector<int>>& result) 
{
    cout << "[";
    for (int i = 0; i < result.size(); ++i) 
    {
        cout << "[";
        for (int j = 0; j < result[i].size(); ++j) 
        {
            cout << result[i][j];
            if (j < result[i].size() - 1) cout << ",";
        }
        cout << "]";
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main()
{
    vector<int> nums1 = {1, 2, 3};
    vector<int> nums2 = {0, 1};

    Solution sol;

    auto vv1 = sol.permute(nums1);
    auto vv2 = sol.permute(nums2);

    printResult(vv1);
    printResult(vv2);

    return 0;
}
